package tgc.edu.zz.controller;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import tgc.edu.zz.form.FilesForm;
import tgc.edu.zz.service.FilesService;
import tgc.edu.zz.utils.AjaxResult;
import tgc.edu.zz.utils.TablePageable;
import tgc.edu.zz.vo.FilesVo;

@Controller
@RequestMapping("/files")
public class FilesController {
		@Autowired
		private FilesService filesService;

	/**
	 * @return 上传页面
	 */
	@RequestMapping("/upload")
	public String upload() {
		return "files/upload";
	}
	
	/**
	 * 
	 * @param form
	 * @param pageParam
	 * @return 所有上传的文件展示
	 */
	@RequestMapping("/list")
	@ResponseBody
	public Object list(FilesForm form,TablePageable pageParam) {
		PageRequest pageable = pageParam.bulidPageRequest();
		Specification<FilesVo> spec = buildSpec(form);
		Page<FilesVo> page=filesService.findAll(spec, pageable);
		return AjaxResult.bulidPageResult(page);
		
	}
	
	private Specification<FilesVo> buildSpec(FilesForm form) {
		return null;
	}
	
	/**
	 * 上传文件页面
	 * @param map
	 * @return
	 */
	@RequestMapping("/edit")
	public String edit(ModelMap map) {
		FilesVo model = new FilesVo();
		map.put("model", model);
		return "files/edit";
	}
	/**
	 * 上传文件
	 * @param request
	 * @param file
	 * @param multipartFile
	 * @return
	 */
	@RequestMapping(value = "/addApk",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addApk(HttpServletRequest request, FilesVo file,@RequestParam( value="file",required=false)MultipartFile multipartFile) {
		filesService.saveApk(multipartFile,file);
		Map<String, Object> json = new HashMap<String, Object>();
        json.put("message", "应用上传成功");
        json.put("status", true);
		return json;
	}
	 
	/**
	 * 下载文件
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/download")
	@ResponseBody
	public Object download(Integer id, HttpServletRequest request, HttpServletResponse response) {
		filesService.download(id, request, response);
		return null;
	}
	
	@RequestMapping(value="/download2")
	public Object download2(Integer id, HttpServletRequest request, HttpServletResponse response) {
		filesService.download(id, request, response);
		return null;
	}

	/**
	 * 删除文件
	 * @param id
	 * @return 
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(Integer id) {
		FilesVo model = filesService.findById(id);
		File file = new File("c:/files/google",model.getUuid());
		if(file.exists()) {
			file.delete();
			System.out.println("存在此文件名："+model.getUuid());
		}
		filesService.deleteById(id);
		return "删除成功！";
	}
	
	/**
	 * 文件下载页面
	 * @param map
	 * @return
	 */
	@RequestMapping("/files")
	public String files(ModelMap map) {
		List<FilesVo> model = filesService.findAll();
		map.put("model", model);
		return "files/files";
	}
	
	
	
	/**
	 * 视频提取界面
	 * @param map
	 * @return
	 */
	@RequestMapping("/video")
	public String video(ModelMap map) {
		return "files/video";
	}
}
